package edu.iscas.appcheckclient.service.check;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Button;
import android.widget.EditText;

import java.util.List;

import edu.iscas.appcheckclient.model.Selector;
import edu.iscas.appcheckclient.model.actions.Action;
import edu.iscas.appcheckclient.model.actions.Check;
import edu.iscas.appcheckclient.model.actions.ParamAction;
import edu.iscas.appcheckclient.model.actions.Perform;
import edu.iscas.appcheckclient.service.FloatingWindowService;
import edu.iscas.appcheckclient.service.RecordService;

public class CheckMenuListener implements View.OnClickListener{

    AccessibilityNodeInfo targetNodeInfo;
    Selector selector;
    List<Action> actionList;
    List<ParamAction> tempActionList;
    RecordService recordService;
    View checkOptionsView;

    final static String[] texts= {
        "Displayed", "Completely displayed", "Enabled", "Not enabled",
            "Focusable", "Not focusable", "Has focus", "Not have focus",
            "Matches text", "Matches text part", "Store text in var","Checked",
            "Not Checked", "Clickable", "Not clickable", "Support IME",
            "Not support IME"
    };

    public CheckMenuListener(AccessibilityNodeInfo targetNodeInfo, Selector selector, List<Action> actionList, View checkOptionsView, List<ParamAction> tempActionList) {
        this.targetNodeInfo = targetNodeInfo;
        this.selector = selector;
        this.actionList = actionList;
        this.checkOptionsView = checkOptionsView;
        this.recordService = RecordService.getRecordService();
        this.tempActionList = tempActionList;
    }

    private void addCheck(String text){
        Check check;
        switch (text) {
            case "Displayed":
                this.actionList.add(new Check(Check.Type.Displayed, this.selector, true));
                break;
            case "Completely displayed":
                this.actionList.add(new Check(Check.Type.CompletelyDisplayed, this.selector, true));
                break;
            case "Enabled":
                this.actionList.add(new Check(Check.Type.Enabled, this.selector, true));
                break;
            case "Not enabled":
                this.actionList.add(new Check(Check.Type.Enabled, this.selector, false));
                break;
            case "Focusable":
                this.actionList.add(new Check(Check.Type.Focusable, this.selector, true));
                break;
            case "Not focusable":
                this.actionList.add(new Check(Check.Type.Focusable, this.selector, false));
                break;
            case "Has focus":
                this.actionList.add(new Check(Check.Type.Focused, this.selector, true));
                break;
            case "Not have focus":
                this.actionList.add(new Check(Check.Type.Focused, this.selector, false));
                break;
            case "Matches text":
                check = new Check(Check.Type.Text, this.selector, true);
                if (isPassword()) {
                    check.addParam("text", "");
                } else {
                    String str = "";
                    if (!TextUtils.isEmpty(this.targetNodeInfo.getText())) {
                        str = this.targetNodeInfo.getText().toString();
                    }
                    check.addParam("text", str);
                }
                check.brewStatement(true);
                this.actionList.add(check);
                break;
            case "Matches text part": {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this.recordService);
                final EditText editText = new EditText(this.recordService);
                String str = "";
                if (!TextUtils.isEmpty(this.targetNodeInfo.getText())) {
                    str = this.targetNodeInfo.getText().toString();
                }
                editText.setText(str);
                alertDialogBuilder.setView(editText);
                alertDialogBuilder.setMessage("Please edit the tet and then select desired operation");
                alertDialogBuilder.setNeutralButton("StartsWith", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Check check = new Check(Check.Type.TextStartsWith, selector, true);
                        check.addParam("text", editText.getText().toString());
                        actionList.add(check);
                    }
                });
                alertDialogBuilder.setNegativeButton("Contains", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Check check = new Check(Check.Type.TextContains, selector, true);
                        check.addParam("text", editText.getText().toString());
                        actionList.add(check);
                    }
                });
                alertDialogBuilder.setPositiveButton("EndsWith", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Check check = new Check(Check.Type.TextEndsWith, selector, true);
                        check.addParam("text", editText.getText().toString());
                        actionList.add(check);
                    }
                });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                alertDialog.show();
                break;
            }
            case "Store text in var": {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this.recordService);
                final EditText editText = new EditText(this.recordService);
                if (!TextUtils.isEmpty(this.targetNodeInfo.getText())) {
                    editText.setText("text");
                    alertDialogBuilder.setView(editText);
                    alertDialogBuilder.setMessage("Enter name of the variable");
                    alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Check check = new Check(Check.Type.TextAsVar, selector, true);
                            check.addParam("variable", editText.getText().toString());
                            actionList.add(check);
                        }
                    });
                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                    alertDialog.show();
                }
                break;
            }
            case "Checked":
                this.actionList.add(new Check(Check.Type.Checked, selector, true));
                break;
            case "Not Checked":
                this.actionList.add(new Check(Check.Type.Checked, selector, false));
                break;
            case "Clickable":
                this.actionList.add(new Check(Check.Type.Clickable, selector, true));
                break;
            case "Not Clickable":
                this.actionList.add(new Check(Check.Type.Clickable, selector, false));
                break;
            case "Support IME":
                this.actionList.add(new Check(Check.Type.SupportsIME, selector, true));
                break;
            case "Not support IME":
                this.actionList.add(new Check(Check.Type.SupportsIME, selector, false));
                break;
        }
    }

    private boolean isPassword(){
        return this.targetNodeInfo.isPassword();
    }
    @Override
    public void onClick(View v) {
        String textOfView = ((Button)v).getText().toString();
        if(RecordService.isChildOfLastParamAction(targetNodeInfo)){
            this.actionList.add(this.tempActionList.get(this.tempActionList.size() - 1));
        }
        if((RecordService.spinnerNode.getAccessibilityNodeInfo() != null) && (!RecordService.spinnerNode.isE())){
            this.actionList.add(new Perform(Perform.Type.Click, RecordService.spinnerNode.getSelector(),
                    RecordService.spinnerNode.getAccessibilityEvent().getEventTime()));
            RecordService.spinnerNode.setE(true);
            RecordService.spinnerNode.updateState();
        }
        Log.d("ViewOptions", "click on options menu - " + textOfView);
        addCheck(textOfView);
        if(textOfView.equals("Cancel Check")){
            //FloatingWindowService.setCheckViewsVisible();
        }else if(textOfView.equals("Check everything")){
            List<String> list = CheckMenuListBuilder.buildList(targetNodeInfo);
            list.remove("Matches text part");
            for (String aList : list) {
                addCheck(aList);
            }
        }
        FloatingWindowService.setCheckViewsVisible();
    }
}
